package com.e0box.game.logicGraphics.data
{
	import com.e0box.game.event.LocalDataServerEvent;
	import com.e0box.game.utils.LocalDataSaver;
	
	import flash.desktop.NativeApplication;
	import flash.utils.ByteArray;
	
	public class PlayerData
	{
		
		public static const FILE_NAME:String = NativeApplication.nativeApplication.applicationID;
		
		private static var _instance:PlayerData;
		
		public var version:String = "0.0.8";
		
		public var uid:String;
		public var hasRestore:Boolean;
		public var installTime:Number;
		public var gameCenterCount:uint;
		public var mute:Boolean;
		public var isCharge:Boolean;
		public var tutorialStep:Array;
		public function PlayerData(single:validate)
		{
			if(!single) throw new Error("Please use the static method 'getInstance'.");
			
			init();
		}
		
		private function init():void{
			
			LocalDataSaver.instance.addEventListener(LocalDataServerEvent.SAVE_COMPLETE, onSaveComplete);
			LocalDataSaver.instance.addEventListener(LocalDataServerEvent.LOAD_COMPLETE, onLoadComplete);
		}
		
		public function save():void{
			var obj:Object = new Object();
			obj.it = installTime;
			obj.ic = isCharge;
			obj.ts = tutorialStep.join(",");
			obj.uid = uid;
			obj.hr = hasRestore;
			obj.mute = mute;
			obj.gcc = gameCenterCount;
			
			
			obj.nTop = new Array();
			obj.tTop = new Array();
			obj.sTop = new Array();
			
			obj.version = this.version;
			
			var bytes:ByteArray = new ByteArray();
			bytes.writeObject(obj);
			LocalDataSaver.instance.saveUserData(FILE_NAME, bytes);
			
		}
		
		public function load():void{
			LocalDataSaver.instance.loadUserData(FILE_NAME);
		}
		
		protected function onLoadComplete(evt:LocalDataServerEvent):void
		{
			if(evt.bytes){
				var obj:Object = evt.bytes.readObject();
				
				if(obj.version == this.version){
					
					isCharge = obj.ic;
					mute = obj.mute;
					installTime = obj.it;
					tutorialStep = String(obj.ts).split(",");
					gameCenterCount = obj.gcc;
					
					uid = obj.uid;
					hasRestore = obj.hr;
					
				}else{
					
					isCharge = obj.ic;
					mute = obj.mute;
					installTime = obj.it ? obj.it : new Date().getTime();
					tutorialStep = obj.ts ? String(obj.ts).split(",") : ["0","0","0","0","0","0","0"];
					gameCenterCount = obj.gcc ? obj.gcc : 10;
					
					uid = obj.uid ? obj.uid : "";
					hasRestore = obj.hr ? obj.hr : "";
				}
				
				trace("load complete");
			}else{
				
				//first save
				//init all player data
				isCharge = false;
				mute = false;
				installTime = new Date().getTime();
				tutorialStep = ["0","0","0","0","0","0","0"];
				gameCenterCount = 10;
				
				hasRestore = false;
				trace("no data,create new one.");
			}
		}
		
		protected function onSaveComplete(evt:LocalDataServerEvent):void
		{
			trace("save complete");
		}
		
		public static function get instance():PlayerData{
			if(!_instance) _instance = new PlayerData(new validate());
			
			return _instance;
		}
	}
}

class validate{
	public function validate(){
		
	}
}